using System.Collections;
using UnityEngine;

public sealed class GlobalInitialization : MonoBehaviour
{
	[SerializeField]
	private LevelDataIndex m_LevelDatas;

	[SerializeField]
	private string m_bgmName;

	private void Awake()
	{
		Screen.sleepTimeout = -1;
		ConstantData.LevelDatas = m_LevelDatas;
		string str = PlayerPrefs.GetString("MissionRecords");
		//ConstantData.missionRecords = JsonUtility.FromJson<MissionRecords>(str);
		if (ConstantData.missionRecords == null) ConstantData.missionRecords = new MissionRecords();
		ConstantData.missionRecords.JsonToObject(str);
		//ConstantData.missionRecords = LoadRecords<MissionRecords>(ConstantData.missionLevelDataRecordPath);

		str = PlayerPrefs.GetString("QuickFirePlayer.PlayerData");
		ConstantData.quickFireRecords = JsonUtility.FromJson<QuickFirePlayer.PlayerData>(str);
		if (ConstantData.quickFireRecords == null) ConstantData.quickFireRecords = new QuickFirePlayer.PlayerData();
		//ConstantData.quickFireRecords = LoadRecords<QuickFirePlayer.PlayerData>(ConstantData.quickFireGameRecordPath);

		str = PlayerPrefs.GetString("AchieveRecords");
		//ConstantData.achieveRecords = JsonUtility.FromJson<AchieveRecords>(str);
		AchieveRecords achieveRecords;
		if (ConstantData.achieveRecords == null)
		{
			achieveRecords = new AchieveRecords();
		}
		else
		{
			achieveRecords = ConstantData.achieveRecords;
		}
		achieveRecords.JsonToObject(str);
		ConstantData.achieveRecords = achieveRecords;
		//ConstantData.achieveRecords = LoadRecords<AchieveRecords>(ConstantData.achieveDataRecordPath);

		str = PlayerPrefs.GetString("GameStatistics");
		ConstantData.gameStatisticsData = JsonUtility.FromJson<GameStatistics>(str);
		if (ConstantData.gameStatisticsData == null) ConstantData.gameStatisticsData = new GameStatistics();
		//ConstantData.gameStatisticsData = LoadRecords<GameStatistics>(ConstantData.gameStatisticsDataPath);
	}

	private void Start()
	{
		//StartCoroutine(LoadPoolAsset(OnPoolAssetLoadedAtAndroidPlatform));
		//AdManager.instance.Init();
		//AdManager.instance.ShowInterstitial(QiXunAd.PAGE_HOME, 0, null);
		ItemInfoMgr.Load();
		BattleInfoMgr.Load();
		ShopInfoMgr.Load();
		LuckyConfig.Load();
		RankConfig.Load();
		CommonConfig.Load();
		DayRecord.instance.Load();
		GiftMgr.Load();
		Object original = Resources.Load("CanvasLaunch");
		Object.Instantiate(original);
		Object original2 = Resources.Load("LuckyCanvas");
		GameObject luckyPanel = UnityEngine.Object.Instantiate(original2) as GameObject;
		LaunchUIController.instance.luckyPanel = luckyPanel;
		Object original3 = Resources.Load("ShopCanvas");
		LaunchUIController.instance.shopCanvas = (UnityEngine.Object.Instantiate(original3) as GameObject);
		if (GameManager.WindowCanvas == null)
		{
			Object original4 = Resources.Load("UI/WindowCanvas");
			GameObject gameObject = UnityEngine.Object.Instantiate(original4) as GameObject;
			GameManager.WindowCanvas = gameObject.GetComponent<Canvas>();
			Object.DontDestroyOnLoad(gameObject);
		}
		AudioManager.PlayBGM(m_bgmName);
	}

	private IEnumerator LoadPoolAsset(Delegate1Args<PoolDataAsset> onloaded)
	{
		WWW www = new WWW(ConstantData.PoolDataAssetsFile);
		UnityEngine.Debug.Log("load file : " + ConstantData.PoolDataAssetsFile);
		yield return www;
		if (string.IsNullOrEmpty(www.error))
		{
			UnityEngine.Debug.Log("load file success");
			onloaded(StreamTools.DeserializeObject<PoolDataAsset>(www.bytes));
		}
		else
		{
			UnityEngine.Debug.LogError(www.error);
		}
	}

	private void OnPoolAssetLoadedAtAndroidPlatform(PoolDataAsset dataAsset)
	{
		ConstantData.SetPoolDatas(dataAsset);
	}

	private T LoadRecords<T>(string path) where T : new()
	{
		T val = StreamTools.DeserializeObject<T>(path);
		if (val == null)
		{
			UnityEngine.Debug.Log("File : " + path + " does not exist, now create.");
			val = new T();
		}
		return val;
	}
}
